import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:imember/UI/Home.dart';
import 'package:imember/Util/Global.dart';
import 'package:imember/Util/MyColor.dart';
import 'package:imember/Util/PageUtil.dart';
import 'package:imember/Widget/MyButton.dart';
import 'package:imember/Widget/MyDialog.dart';
import 'package:imember/Widget/MyImage.dart';
import 'package:imember/Widget/MyInput.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Login extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return LoginState();
  }
}

class LoginState extends State<Login> {
  TextEditingController accountController = TextEditingController();
  TextEditingController pswController = TextEditingController();

  @override
  void initState() {
    clearToken();
    super.initState();
  }

  clearToken() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString("token", "");
    Global.token = "";
  }

  Future<void> LoginClick() async {
    PageUtil().open(context, Home());
    String account = accountController.text;
    String psw = pswController.text;
    if (account.isEmpty || psw.isEmpty) {
      return;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: MyColor.White,
        body: Container(
            width: double.infinity,
            child: Column(
              children: [
                Expanded(
                    child: Column(crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [
                  LocalImage("ic_logo.png", width: 390),
                  Container(
                      height: 48,
                      width: 395,
                      decoration: Global.getBoxBg(24, MyColor.F5),
                      margin: EdgeInsets.only(top: 58),
                      padding: EdgeInsets.fromLTRB(18, 0, 18, 0),
                      child: NormalInput(hintText: "商家代碼", fontSize: 20)),
                  Container(
                      height: 48,
                      width: 395,
                      decoration: Global.getBoxBg(24, MyColor.F5),
                      padding: EdgeInsets.fromLTRB(18, 0, 18, 0),
                      margin: EdgeInsets.only(top: 18),
                      child: NormalInput(hintText: "帳號", fontSize: 20)),
                  Container(
                      height: 48,
                      width: 395,
                      decoration: Global.getBoxBg(24, MyColor.F5),
                      padding: EdgeInsets.fromLTRB(18, 0, 18, 0),
                      margin: EdgeInsets.only(top: 18, bottom: 72),
                      child: NormalInput(hintText: "密碼", fontSize: 20)),
                  OrangeBtn("登入", width: 128,onTap: LoginClick),
                ])),
                LocalImage("ic_login_bottom.png")
              ],
            )));
  }
}
